﻿using System;
using System.Collections.Generic;
using System.Text;
using AudioPlayer.Plugin.Playback;
using AudioPlayer.Plugin.Storage;

namespace AudioPlayer.Plugin {
	/// <summary>
	/// Random playback controller
	/// </summary>
	internal class PlayControl : IPlayControl,IPluginHost {
		#region Fields
		private IPlaylist _playlist;//active playlist
		private ISong _previousSong;
		private ISong _activeSong;
		private IHost _host;

		private bool _subscribed;//True if controller is subscribed to playback events.
		#endregion
		#region Constructor
		public PlayControl() {
			
		}
		#endregion
		#region Private Voids
		private void InitHost() {
			//TODO: Fix up
		}

		private void Host_PluginLoad(PluginBase obj) {
			this.CheckStorage();
		}
		private void CheckStorage() {
			/*if (this.Host.Storage != null) {
				if (this.Host.Storage.Library != null) {
					_playlist = this.Host.Storage.Library;
				}
			}*/
		}

		private void InitEvents() {
			if (!_subscribed) {
				if (Config.Plugins.HasPlayback) {
					Config.Plugins.PlaybackAudio.Ending += new EventHandler(PlayControl_Ending);
					Config.Plugins.PlaybackAudio.Starting += new EventHandler(PlayControl_Starting);
					_subscribed = true;
				}
			}
		}
		#endregion
		#region Public Voids
		public void Play() {
			this.Next();
		}
		public void Play(ISong item) {
			if (Config.Plugins.HasPlayback) {
				try {
					this.InitEvents();
					Config.Plugins.PlaybackAudio.Load(item.Filename);
					Config.Plugins.PlaybackAudio.Play();

					//set previous song
					_previousSong = this.Active;
					//set active song
					this.Active = item;
				} catch {
					//Mysterious stuff might happen
				}
			}
		}
		public void Play(string url) {
			if (Config.Plugins.HasPlayback) {
				try {
					InitEvents();
					Config.Plugins.PlaybackAudio.Load(url);
					Config.Plugins.PlaybackAudio.Play();
				} catch {
					//Mysterious stuff might happen
				}
			}
		}
		public void Next() {
			//Let's use some randomness to play items
			if (_playlist != null) {
				if (_playlist.Count > 0) {
					Random r = new Random();
					//Get random song
					ISong song = _playlist[r.Next(0, _playlist.Count)];
					if (song != null) {
						this.Play(song);
					}
				}
			}
		}
		public void Prev() {

		}
		public void Stop() {
			if (Config.Plugins.HasPlayback) {
				try {
					Config.Plugins.PlaybackAudio.Stop();
				} catch {

				}
			}
		}
		public void Seek(int perc, System.IO.SeekOrigin seek) {
			if (Config.Plugins.HasPlayback) {
				try {
					Config.Plugins.PlaybackAudio.Seek(perc, seek);
				} catch {

				}
			}
		}
		public void Pause() {
			if (Config.Plugins.HasPlayback) {
				try {
					Config.Plugins.PlaybackAudio.Stop();
				} catch {

				}
			}
		}
		public void Dispose() {
			
		}
		#endregion
		#region Property
		public IPlaylist Playlist {
			get {
				return _playlist;
			}
			set {
				_playlist = value;
			}
		}

		public ISong Active {
			get { return _activeSong; }
			set { _activeSong = value; }
		}

		/// <summary>
		/// Gets a value indicating whether this instance is playing.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is playing; otherwise, <c>false</c>.
		/// </value>
		public bool IsPlaying {
			get {
				return Config.Plugins.PlaybackAudio.IsPlaying;
			}
		}

		public double Duration {
			get { return Config.Plugins.PlaybackAudio.Duration; }
		}
		public double Current {
			get { return Config.Plugins.PlaybackAudio.Current; }
		}
		#endregion
		#region Events
		public event EventHandler Starting;
		public event EventHandler Ending;

		protected virtual void OnStarting() {
			if (Starting != null) {
				this.Starting(this, EventArgs.Empty);
			}
		}
		protected virtual void OnEnding() {
			if (Ending != null) {
				this.Ending(this, EventArgs.Empty);
			}
		}
		#endregion
		#region Event Handling
		private void PlayControl_Starting(object sender, EventArgs e) {
			this.OnStarting();
		}
		private void PlayControl_Ending(object sender, EventArgs e) {
			this.OnEnding();
		}
		#endregion
		#region IPluginHost Members

		/// <summary>
		/// Gets or sets the host.
		/// After plugin loaded, the host will be set automaticaly.
		/// </summary>
		/// <value>The Host.</value>
		public IHost Host {
			get {
				return _host;
			}
			set {
				_host = value;
				InitHost();
			}
		}
		#endregion
	}
}
